package com.yfp.tissue.service.impl;

import com.yfp.common.core.domain.R;
import com.yfp.common.core.utils.uuid.Snowflake;
import com.yfp.common.security.utils.SecurityUtils;
import com.yfp.system.api.model.LoginUser;
import com.yfp.tissue.domain.entity.Position;
import com.yfp.tissue.domain.entity.PositionType;
import com.yfp.tissue.mapper.PositionMapper;
import com.yfp.tissue.mapper.PositionTypeMapper;
import com.yfp.tissue.service.PositionTypeService;
import jakarta.annotation.Resource;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @PROJECT_NAME: ydyfp
 * @DESCRIPTION:
 * @USER: xiaohuya
 * @DATE: 2023/12/19 16:05
 */

@Service
public class PositionTypeServiceImpl implements PositionTypeService {
    @Resource
    private PositionTypeMapper positionTypeMapper;
    @Resource
    private PositionMapper positionMapper;

    @Override
    public List<PositionType> queryDetail(PositionType positionType) {
        List<PositionType> positionTypes = positionTypeMapper.queryList(positionType);
        return positionTypes;
    }

    @Override
    public R addPositionType(PositionType positionType) {
        Snowflake snowflake = new Snowflake(1);
        positionType.setPositionTypeId(String.valueOf(snowflake.nextId()));
        PositionType positionTypeNull = new PositionType();
        positionTypeNull.setCityName(positionType.getCityName());
        List<PositionType> positionTypes = positionTypeMapper.queryById(positionTypeNull);
        List<PositionType> list = positionTypes.stream().filter(e -> e.getTypeName().equals(positionType.getTypeName())).toList();
        if (list.size() > 0) {
            return R.fail("点位类型名称重复");
        }
        LoginUser loginUser = SecurityUtils.getLoginUser();

        positionType.setUpdateName(loginUser.getSysUser().getNickName());
        positionType.setEnable("1");
        positionType.setIsDelete(false);
        positionType.setDeptId(loginUser.getSysUser().getDeptId());
        positionTypeMapper.insert(positionType);
        return R.ok();
    }

    @Override
    public R updatePositionType(PositionType positionType) {
        LoginUser loginUser = SecurityUtils.getLoginUser();
        PositionType positionTypeNull = new PositionType();
        positionTypeNull.setCityName(positionType.getCityName());
        List<PositionType> positionTypes = positionTypeMapper.queryById(positionTypeNull);
        for (PositionType type : positionTypes) {
            if (type.getTypeName().equals(positionType.getTypeName())) {
                if (!type.getPositionTypeId().equals(positionType.getPositionTypeId())) {
                    return R.fail("点位类型名称重复");
                }
            }
        }
        positionType.setUpdateName(loginUser.getSysUser().getNickName());

        positionTypeMapper.update(positionType);

        return R.ok();
    }

    @Override
    public R updatePositionTypeStatus(PositionType positionType) {
        LoginUser loginUser = SecurityUtils.getLoginUser();

        positionType.setUpdateName(loginUser.getSysUser().getNickName());
        Position position = new Position();
        position.setPositionTypeId(positionType.getPositionTypeId());
        long count = positionMapper.count(position);
        if (count >= 1 && positionType.getEnable().equals("0")) {
            return R.fail("该类型被使用中禁止禁用");
        }
        positionTypeMapper.updateEnable(positionType);

        return R.ok();
    }
}
